import gesture_sensor as gs
from urandom import randint
import st7789
import utime
place=[(0,0),(40,0),(80,0),(120,0),(0,40),(40,40),(80,40),(120,40),(0,80),(40,80),(80,80),
           (120,80),(0,120),(40,120),(80,120),(120,120)]
def switch(themap, loc):
    if themap[loc]=='<':
        themap[loc]='>'
    elif themap[loc]=='>':
        themap[loc]='<'
    elif themap[loc]=='^':
        themap[loc]='v'
    elif themap[loc]=='v':
        themap[loc]='^'
    return themap

def mkmap(oldtime,count):
    themap=['>','v','<','^','<','>','^','v','^','v','<','>','v','<','^','>']
    x=utime.ticks_diff(utime.ticks_ms(),oldtime)%9
    if count==1:
        themap=switch(themap,x)
    elif count==2:
        y=x+2
        themap=switch(themap,x)
        themap=switch(themap,y)
    elif count==3:
        y=x+1
        z=x+3
        themap=switch(themap,x)
        themap=switch(themap,y)
        themap=switch(themap,z)
    elif count==4:
        y=x+2
        z=x+4
        a=x+6
        themap=switch(themap,x)
        themap=switch(themap,y)
        themap=switch(themap,z)
        themap=switch(themap,a)
    elif count==5:
        y=x+1
        z=x+3
        a=x+5
        b=x+7
        themap=switch(themap,x)
        themap=switch(themap,y)
        themap=switch(themap,z)
        themap=switch(themap,a)
        themap=switch(themap,b)
    return themap
def gamelogin(display):
    display.fill(st7789.color565(50,100,200))
    display.draw_string(20, 70, 'right to start,', color=st7789.color565(255, 255, 255),\
                        bg=st7789.color565(50,100,200), size=2)
    display.draw_string(20, 100, 'left ro see the', color=st7789.color565(255, 255, 255),\
                        bg=st7789.color565(50,100,200), size=2)
    display.draw_string(20, 130, 'game instructions', color=st7789.color565(255, 255, 255),\
                        bg=st7789.color565(50,100,200), size=2)
    while True:
        ges=gs.toget()
        if ges=='right':
            return 1
        elif ges=='left':
            return -1
        utime.sleep_ms(500)
def gameinstruction(display):
    display.fill(st7789.color565(0,150,50))
    display.draw_string(10,20,'up to switch direc,',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    display.draw_string(10,53,'right to go forward',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    display.draw_string(10,86,'left to go back',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    display.draw_string(10,119,'make the whole map',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    display.draw_string(10,152,'centrosymmetric',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    display.draw_string(10,200,'down to return',color=st7789.color565(200,100,255),\
                        bg=st7789.color565(0,0,0),size=2)
    while True:
        ges=gs.toget()
        if ges=='down':
            print(ges)
            return 0
        utime.sleep_ms(200)
def draw(display,themap,loc,step):
    display.fill(st7789.color565(150,200,200))
    for i in range(3):
        display.hline(10,50+40*i,160,st7789.color565(0,0,0))
    for i in range(3):
        display.vline(50+40*i,10,160,st7789.color565(0,0,0))
    for i in range(len(themap)):
        x=place[i][0]+20
        y=place[i][1]+20
        if themap[i]=='<' and loc!=i:
            display.draw_string(x,y,themap[i],color=st7789.color565(0,0,255),\
                                bg=st7789.color565(150,200,200),size=3)
        elif themap[i]=='<' and loc==i:
            display.draw_string(x,y,themap[i],color=st7789.color565(0,0,255),\
                                bg=st7789.color565(100,100,100),size=3)
        elif themap[i]=='>' and loc!=i:
            display.draw_string(x,y,themap[i],color=st7789.color565(0,255,0),\
                                bg=st7789.color565(150,200,200),size=3)
        elif themap[i]=='>' and loc==i:
            display.draw_string(x,y,themap[i],color=st7789.color565(0,255,0),\
                                bg=st7789.color565(100,100,100),size=3)
        elif themap[i]=='v' and loc!=i:
            display.draw_string(x,y,themap[i],color=st7789.color565(50,125,200),\
                                bg=st7789.color565(150,200,200),size=3)
        elif themap[i]=='v' and loc==i:
            display.draw_string(x,y,themap[i],color=st7789.color565(50,125,200),\
                                bg=st7789.color565(100,100,100),size=3)
        elif themap[i]=='^' and loc!=i:
            display.draw_string(x,y,themap[i],color=st7789.color565(200,125,50),\
                                bg=st7789.color565(150,200,200),size=3)
        elif themap[i]=='^' and loc==i:
            display.draw_string(x,y,themap[i],color=st7789.color565(200,125,50),\
                                bg=st7789.color565(100,100,100),size=3)
    display.draw_string(30,195,'Step : '+str(step),color=st7789.color565(200,55,55),\
                        bg=st7789.color565(150,200,200),size=3)
def gamepass(themap):
    i=0;j=15
    while i<j:
        if themap[i]==themap[j]:
            return False
        i+=1
        j-=1
    return True